# LAN Matchmaking

from scripts import *;
from WtfEngine import *;
from WtfNet import *;
import WtfGui;

gPort = 1337;

class Matchmaker:
	def __init__(self):
		self.inTransaction = 0;
		self.sockets = [];
		
		self.broadcastSocket = UdpBroadcastSocket(gPort);
		self.listenSocket = UdpSocket(gPort);
		getState().BindEvent(SocketDataEvent(self.listenSocket), EventHandler(self.CatchBroadcast));
		
	def Start(self):
		self.broadcastSocket.SendPacket(StringPacket("Hello"));
		self.listenSocket.Listen();
	
	def CatchBroadcast(self, e):
		# Get source IP of the broadcast and attempt to connect.
		DebugPrint("Caught a broadcast");

class Lobby(WtfGui.Window):
	def __init__(self):
		WtfGui.Window.__init__(self, "LAN Lobby");
		
		font = WtfGui.Font("v_gui_font");
		
		cell = WtfGui.Label("Data ", font);
		cell2 = WtfGui.Label("100 ", font);
		
		self.table = WtfGui.Table(WtfGui.LayoutContainerCreationOptions(), 1);
		self.table.AddHeading(WtfGui.TableHeading(300, WtfGui.Label("Gamer ID ", font)));
		self.table.AddHeading(WtfGui.TableHeading(100, WtfGui.Label("Ping ", font)));
		
		row = self.table.AddRecord();
		row.AddChild(cell);
		row.AddChild(cell2);
		row = self.table.AddRecord();
		row.AddChild(cell);
		row.AddChild(cell2);
		row = self.table.AddRecord();
		row.AddChild(cell);
		row.AddChild(cell2);
		
		self.table.size = self.innerSize;
		self.AddChild(self.table);
		
def showLobby(wm):
	wm.AddWindow(Lobby(), 1);
			
def test():
	sock = TcpSocket(80);
	getState().BindEvent(SocketClientConnectEvent(sock), EventHandler(
		lambda e: e.newSocket.SendPacket(StringPacket("HTTP/1.1 200 OK\nContent-Type: text/html\nContent-Length: 12\n\nHello world!"))
	));
	sock.Listen();
